#include "Water.h"
#include "s3e.h"
#include "Iw2D.h"
#include "il_assert.h"
#include <math.h>

void Water::Load(const char* TextureA, const char* TextureB)
{
	m_layers = 2;
	WaterTile[0].Load( TextureA );
	WaterTile[1].Load( TextureB );
	
	m_pos[0] = CIwFVec2(0,0);
	m_pos[1] = CIwFVec2(-6,-6);
}

void Water::Update( float dt )
{
	m_pos[0].x += ((float)(rand() % 10)/10.0f)*-1.0f;
	m_pos[0].y += ((float)(rand() % 10)/10.0f)/2.0f-0.25f;
	if(m_pos[0].x<-WATER_TILE_SIZE_X)
	{
		m_pos[0].x = 0;
	}
	if(m_pos[0].y>0)
	{
		m_pos[0].y = -31;
	}

	if(m_layers==1) return;

	m_pos[1].x += ((float)(rand() % 10)/10.0f)*1.0f;
	m_pos[1].y += ((float)(rand() % 10)/10.0f)/2.0f-0.1f;

	if(m_pos[1].x>0)
	{
		m_pos[1].x = -WATER_TILE_SIZE_X;
	}
	if(m_pos[1].y>0)
	{
		m_pos[1].y = -WATER_TILE_SIZE_Y;
	}
	if(m_pos[1].x<-WATER_TILE_SIZE_X)
	{
		m_pos[1].x = 0;
	}
	if(m_pos[1].y<-WATER_TILE_SIZE_Y)
	{
		m_pos[1].y = 0;
	}
}

void Water::Draw()
{
	for(int wy = (int)m_pos[0].y; wy < 320; wy += WATER_TILE_SIZE_Y)
	{
		for(int wx = (int)m_pos[0].x; wx < 480; wx += WATER_TILE_SIZE_X)
		{
			WaterTile[0].Draw(CIwSVec2(wx,wy));
		}
	}

	if(m_layers==1) return;

	for(int wy = (int)m_pos[1].y; wy < 320; wy += WATER_TILE_SIZE_Y)
	{
		for(int wx = (int)m_pos[1].x; wx < 480; wx += WATER_TILE_SIZE_X)
		{
			WaterTile[1].Draw(CIwSVec2(wx,wy));
		}
	}
}

void Water::Unload()
{
	WaterTile[0].Unload();
	if(m_layers==1) return;
	WaterTile[1].Unload();
	if(m_layers==2) return;
	WaterTile[2].Unload();
	if(m_layers==3) return;
	WaterTile[3].Unload();
	if(m_layers==4) return;
	WaterTile[4].Unload();
}